﻿<?php


/*
 * na podtsawie pliku: http://boinc.berkeley.edu/android-boinc/boinc/lib/msg_queue.cpp
 */


function create_message_queue($key) {
        $mq_id = 0;


        $mq_id = msg_get_queue($key, 0666);
        if ($mq_id == FALSE) { 
                debug_log("create_message_queue: msgget- ".posix_strerror(posix_get_last_error()));
                return -1;
        }
        return $mq_id;
}


function receive_message($key, &$msg, $msg_size = 1024, $wait = true) {
        $mq_id = 0;
        $retval = 0;
        $err = 0;
        $msgtype = 0;
        $desiredmsgtype=0;
        $unserialize=true;
        $mq_id = msg_get_queue($key, 0666);
        if ($mq_id == FALSE) {
                debug_log("receive_message: msgget- ".posix_strerror(posix_get_last_error()));
                return -1;
        }
        //msg_receive ( resource $mq_id , int $desiredmsgtype , int &$msgtype , int $maxsize , mixed &$message , bool $unserialize = true , int $flags = 0 [, int &$errorcode ]]] )
        $retval = msg_receive($mq_id, $desiredmsgtype, $msgtype, $msg_size, $msg, $unserialize, ($wait ? 0 : MSG_IPC_NOWAIT), $err);
        if ($retval == FALSE) {
                debug_log("receive_message: msgrcv - ".posix_strerror($err));
                return -1;
        }


        return 0;
}


function send_message($key, &$msg,$msgtype=1) {
        $serialize = TRUE;
        $blocking  = TRUE;
        $mq_id = 0;
        $retval = 0;
        $err = 0;


        $mq_id = msg_get_queue($key, 0666);
        if ($mq_id == FALSE) {
                debug_log("send_message: msgget- ".posix_strerror(posix_get_last_error()));
                return -1;
        }
        $retval = msg_send($mq_id, $msgtype, $msg, $serialize , $blocking, $err);
        if ($retval == FALSE) {
                debug_log("send_message: msgsnd- ".posix_strerror($err));
                return -1;
        }


        return 0;
}


function destroy_message_queue($key) {
        $mq_id = 0;
        $retval = 0;


        $mq_id = msg_get_queue($key, 0666);
        if ($mq_id == FALSE) {
                debug_log("delete_message_queue: msgget- ".posix_strerror(posix_get_last_error()));
                return -1;
        }
        $retval = msg_remove_queue($mq_id);
        if ($retval == FALSE) {
                debug_log("delete_message_queue: msgctl- ".posix_strerror(posix_get_last_error()));
                return -1;
        }
        return 0;
}
?>